package com.vhall.component.service.room.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vhall.component.dao.room.RoomWhitePhoneRelationMapper;
import com.vhall.component.entity.room.entity.RoomWhitePhoneRelationEntity;
import com.vhall.component.plugin.common.constant.RedisKey;
import com.vhall.component.plugin.common.utils.RedisLocker;
import com.vhall.component.service.room.RoomWhitePhoneRelationService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* @author a1111
* @description 针对表【room_white_phone_relation(房间和白名单的关联表,设置可观看用户范围)】的数据库操作Service实现
* @createDate 2023-10-26 13:26:34
*/
@Service
public class RoomWhitePhoneRelationServiceImpl extends ServiceImpl<RoomWhitePhoneRelationMapper, RoomWhitePhoneRelationEntity>
    implements RoomWhitePhoneRelationService {
    @Resource
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public void batchChangeRoomWhitePhoneRelationInfo(Integer whiteFileId, Integer ilId, Integer roomDraftRecordId) {
        LambdaQueryWrapper<RoomWhitePhoneRelationEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RoomWhitePhoneRelationEntity::getRoomWhiteFileId, whiteFileId);
        List<RoomWhitePhoneRelationEntity> whitePhoneList = list(queryWrapper);
        whitePhoneList = whitePhoneList.stream().map(data->{
            data.setIlId(ilId);
            data.setRoomDraftRecordId(roomDraftRecordId);
            return data;
        }).collect(Collectors.toList());
        updateBatchById(whitePhoneList);
        redisTemplate.delete(RedisKey.ALL_ROOM_WHITE_PHONE_SET);
    }

    @Override
    public Set<String> getCacheAllWhitePhoneList() {
        Set<String> phoneSet = new HashSet<>() ;

        if (redisTemplate.hasKey(RedisKey.ALL_ROOM_WHITE_PHONE_SET)) {
            phoneSet = redisTemplate.opsForSet().members(RedisKey.ALL_ROOM_WHITE_PHONE_SET);
        }else{
            if (RedisLocker.tryLock(RedisKey.ALL_ROOM_WHITE_PHONE_SET,"1",1000)) {
                LambdaQueryWrapper<RoomWhitePhoneRelationEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.ne(RoomWhitePhoneRelationEntity::getIlId,0);
                List<RoomWhitePhoneRelationEntity> list = list(queryWrapper);
                phoneSet = list.stream().map(RoomWhitePhoneRelationEntity::getPhone).collect(Collectors.toSet());
                if (CollUtil.isNotEmpty(phoneSet)) {
                    redisTemplate.opsForSet().add(RedisKey.ALL_ROOM_WHITE_PHONE_SET,phoneSet.toArray(String[]::new));
                    redisTemplate.expire(RedisKey.ALL_ROOM_WHITE_PHONE_SET,1, TimeUnit.MINUTES);
                }else{
                    redisTemplate.opsForSet().add(RedisKey.ALL_ROOM_WHITE_PHONE_SET,"");
                    redisTemplate.expire(RedisKey.ALL_ROOM_WHITE_PHONE_SET,1, TimeUnit.MINUTES);
                }


            }
            RedisLocker.unlock(RedisKey.ALL_ROOM_WHITE_PHONE_SET,"1");
        }

        return phoneSet;
    }

    public static void main(String[] args) {
        Set<String> phoneSet = new HashSet<>();
        String[] strings = phoneSet.toArray(String[]::new);
    }

    @Override
    public boolean saveBatch(Collection<RoomWhitePhoneRelationEntity> entityList) {
        if (CollUtil.isNotEmpty(entityList)) {
            redisTemplate.delete(RedisKey.ALL_ROOM_WHITE_PHONE_SET);
        }
        return super.saveBatch(entityList);
    }
}




